package com.gange.utils;

import com.gange.entity.Category;
import com.gange.entity.User;
import com.gange.entity.Video;
import com.gange.feignclients.CategoryClients;
import com.gange.feignclients.UserClients;
import com.gange.vo.VideoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class VideoUtils {
    @Autowired
    private UserClients userClients;
    @Autowired
    private CategoryClients categoryClients;
    public List<VideoVO> VideoListToVideoVoList(List<Video> videoList){
        if(!ObjectUtils.isEmpty(videoList)){
            List<VideoVO> videoVOS = new ArrayList<>();
            videoList.forEach((video)->{
                videoVOS.add(videoToVideovo(video));
            });
            return videoVOS;
        }
        return null;
    }

    public VideoVO videoToVideovo(Video video){
        //创建VideoVo
        VideoVO videoVO = new VideoVO();
        //复制属性
        BeanUtils.copyProperties(video, videoVO);//复制属性

        //视频服务----->调用用户服务 根据用户id查询用户
        User user = userClients.user(video.getUid().toString());//调用用户服务
        log.info("根据id查询用户信息为: {}", JSONUtils.writeValueAsString(video));
        videoVO.setUploader(user.getName());//设置用户名

        //视频服务---->调用类别服务  根据类别id查询类别
        Category category = categoryClients.category(video.getCategoryId().toString());
        log.info("根据类别id查询到的类别信息为: {}", JSONUtils.writeValueAsString(category));
        videoVO.setCategory(category.getName());

        //设置点赞数量
        videoVO.setLikes(0);

        return videoVO;
    }
}
